package cn.wolfcode.wolf2w.util;

import cn.wolfcode.wolf2w.query.QueryObject;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * 数据库操作的工具类
 */
public class DBHelper {

    public static<T> Page<T> query(MongoTemplate template,Pageable pageable,Query query,Class<T> clz,QueryObject qo) {
        // 查询总条数
        long count = template.count(query, clz);
        if(count == 0){
            return Page.empty();
        }
        //Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        query.with(pageable);
        List<T> list = template.find(query, clz);
        return new PageImpl<T>(list,pageable,count);
    }
}
